//
//  ZNWBRefreshView.swift
//  ZNWeibo
//
//  Created by FunctionMaker on 2017/8/28.
//  Copyright © 2017年 FunctionMaker. All rights reserved.
//

import UIKit

/// 下拉刷新显示视图
class ZNWBRefreshView: UIView {
    
    @IBOutlet weak var tipImage: UIImageView?
    @IBOutlet weak var tipIndicator: UIActivityIndicatorView?
    
    @IBOutlet weak var tipLabel: UILabel?
    
    /// 父视图高度
    var parentViewHeight: CGFloat = 0
    
    var state: ZNWBRefreshState = .Normal {
        didSet {
            switch state {
            case .Normal:
                tipImage?.isHidden = false
                tipIndicator?.stopAnimating()
                tipLabel?.text = "继续使劲拉"
                
                /*
                    UIView封装的旋转动画特点：
                    - 默认为顺时针旋转
                    - 就近原则，如果要实现180°旋转，需要减去一个微量，保证逆时针返回更近
                    - 要实现360°旋转，只能使用CA
                 */
                UIView.animate(withDuration: 0.25) {
                    self.tipImage?.transform = CGAffineTransform.identity
                }
            case .Pulling:
                tipLabel?.text = "放手开始刷新"
                
                UIView.animate(withDuration: 0.25) {
                    self.tipImage?.transform = CGAffineTransform(rotationAngle: .pi - 0.001)
                }
            case .willRefresh:
                tipLabel?.text = "正在刷新中..."
                
                // 隐藏提示图标
                tipImage?.isHidden = true
                tipIndicator?.startAnimating()   
            }
        }
    }
    
    class func refreshView() -> ZNWBRefreshView {
        let nib = UINib(nibName: "ZNWBMeiTuanRefreshView", bundle: nil)
        
        return nib.instantiate(withOwner: nil
            , options: nil)[0] as! ZNWBRefreshView
    }
}
